<template>
  <div class="loginWrap">
    <div>
      <h3>登录</h3>
      账号：<input type="text" v-model="form.iphone" /><br />
      密码：<input type="text" v-model="form.password" /><br />
      <el-button type="primary" @click="login">登录</el-button>
      <el-button type="primary" @click="register">注册</el-button>
    </div>
  </div>
</template>

<script>
import {loginApi}  from "../api/user.js"
export default {
  data() {
    return {
      form: {
        iphone: "",
        password: "",
      },
    };
  },
  methods: {
    async login() {
      if (this.form.iphone != "" && this.form.password != "") {
        let res = await loginApi({
          data: this.form,
        });
        if (res.code == 200) {
          alert("登录成功");
          localStorage.setItem("iphone", this.form.iphone);
          localStorage.setItem("token", res.token);
          this.$router.push("/");
          this.form.iphone = "";
          this.form.password = "";
        } else {
          alert(res.msg);
        }
      } else {
        alert("账号或密码不能为空");
      }
    },
    register() {
      this.$router.push("/register");
    },
  },
};
</script>

<style lang="scss" scoped>
.loginWrap {
  > div {
    width: 500px;
    height: 400px;
    box-shadow: 0px 0px 5px #333;
    margin: 0 auto;
    margin-top: 150px;
    text-align: center;
    h3 {
      padding: 30px 0;
      margin-bottom: 40px;
    }
    > input {
      width: 200px;
      height: 30px;
      margin-top: 20px;
    }
    > button {
      margin-top: 40px;
      &:nth-of-type(1) {
        margin-left: 40px;
      }
      &:nth-of-type(2) {
        margin-left: 40px;
      }
    }
  }
}
</style>